import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/button/radius_button.dart';
import 'package:boss/ui/widget/modal/modal.dart';
import 'package:boss/ui/widget/shadow_radius_card.dart';
import 'package:boss/util/widget_to_local.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:qr_flutter/qr_flutter.dart';

Future<void> showCouponSharePostersModal(BuildContext context, String shareUrl) {
  return showModal(
    context,
    builder: (ctx) => _CouponSharePosters(shareUrl),
  );
}

class _CouponSharePosters extends StatefulWidget {
  final String shareUrl;

  const _CouponSharePosters(this.shareUrl, {Key key}) : super(key: key);

  @override
  __CouponSharePostersState createState() => __CouponSharePostersState();
}

class __CouponSharePostersState extends State<_CouponSharePosters> {
  final GlobalKey globalKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return RepaintBoundary(
      key: globalKey,
      child: Container(
        width: 600.w,
        height: 830.w,
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/image/coupon_share_bg.png'),
            fit: BoxFit.fill,
          ),
        ),
        child: Column(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(
                top: 30.w,
              ),
              child: Text(
                '分享越多好友\r\n一起分享美食',
                style: TextStyleConstant().white_26,
              ),
            ),
            Container(
              width: 470.w,
              height: 650.w,
              margin: EdgeInsets.only(
                top: 35.w,
              ),
              decoration: BoxDecoration(
                boxShadow: [DEFAULT_SHADOW],
                image: DecorationImage(
                  image: AssetImage('assets/image/coupon_share_content_bg.png'),
                  fit: BoxFit.fill,
                ),
              ),
              child: Column(
                children: <Widget>[
                  Expanded(
                    flex: 185,
                    child: Container(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Text(
                            '扫码即得优惠劵，快快领取吧',
                            style: TextStyleConstant().blue_30,
                          ),
                          Text(
                            '分享优惠劵，与ta一起领取',
                            style: TextStyleConstant().normal_1_22,
                          ),
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    flex: 465,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Column(
                          children: <Widget>[
                            Text(
                              '扫码领券',
                              style: TextStyleConstant().blue_26,
                            ),
                            Container(
                              margin: EdgeInsets.only(
                                top: 20.w,
                              ),
                              child: QrImage(
                                data: widget.shareUrl,
                                size: 230.w,
                                padding: EdgeInsets.zero,
                              ),
                            ),
                          ],
                        ),
                        RadiusButton(
                          title: '保存至相册',
                          width: 360.w,
                          height: 70.w,
                          onTap: widget2local,
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Future<Uint8List> widget2Unit8List() async {
    BuildContext buildContext = globalKey.currentContext;

    if (buildContext != null) {
      RenderRepaintBoundary boundary = buildContext.findRenderObject();
      final image = await boundary.toImage(pixelRatio: 3.0);
      ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);

      byteData.buffer.asUint8List();

      return byteData.buffer.asUint8List();
    }
  }

  widget2local() async {
    Widget2Local.save(globalKey);
  }
}

class CouponShareUtil {
  static OverlayEntry _entry;
  static GlobalKey<__CouponSharePostersState> _globalKey;

  static savePhoto(BuildContext context) async {}

  static _insert(BuildContext context, String url) {
    _globalKey = GlobalKey<__CouponSharePostersState>();

    _entry = OverlayEntry(builder: (context) {
      return Material(
        color: Colors.transparent,
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Opacity(
              opacity: 0.005,
              child: _CouponSharePosters(
                url,
                key: _globalKey,
              ),
            )
          ],
        ),
      );
    });

    Overlay.of(context).insert(_entry);
  }

  static widget2Unit8List(BuildContext context, String url, ValueChanged<Uint8List> callback) async {
    _insert(context, url);

    Future.delayed(Duration(milliseconds: 300), () async {
      try {
        final data = await _globalKey.currentState.widget2Unit8List();

        callback(data);

        _entry.remove();
      } catch (e) {
        _entry.remove();
      }
    });
  }
}
